<template>
    <div>
        <nav-header title="个人资料" iconnam=""></nav-header>
        <div class="mylist">
            <van-cell title="头像" size="large" center>
                <!-- 使用 right-icon 插槽来自定义右侧图标 -->
                <template #right-icon>
                    <!-- 上传的功能    -->
                    <!--  input定位定到img上   透明度0 -->
                    <input id="file" @change="uploadImg" type="file" style="display: none" />

                    <label for="file">
                        <van-image round width="60" height="60" :src="userInfo.avatar" />
                    </label>
                </template>
            </van-cell>



            <span class="tou">
                <p>昵称</p>
                <div>
                    <input type="text" id="nickname" placeholder="请输入你的昵称" v-model="userInfo.nickname" />
                    <van-icon name="arrow" size="30px" class="top" />
                </div>
            </span>
            <!-- 性别 -->
            <span class="tou">
                <p>性别</p>
                <div>
                    <input type="text" name="sex" id="sex" v-model="userInfo.sex" />
                    <van-icon name="arrow" size="30px" class="top" />
                </div>
            </span>
            <!-- 所在地 -->
            <span class="tou">
                <p>生日</p>
                <div>
                    <input type="text" placeholder="" v-model="userInfo.birthday" id="birthday" />
                    <van-icon name="arrow" size="30px" class="top" />
                </div>
            </span>
            <!-- 手机号 -->
            <span class="tou">
                <p>更改手机号</p>
                <div>
                    <input type="text" placeholder="" v-model="userInfo.username" id="username" />
                    <van-icon name="arrow" size="30px" class="top" />
                </div>
            </span>
        </div>
        <div class="btn" @click="onSubmit()">确认提交</div>
    </div>
</template>

<script>
import NavHeader from "../components/Nav-Header.vue";

export default {
    name: "App02Mylist",
    components: {
        NavHeader,
    },
    data() {
        return {
            value: "",
            userInfo: [],
        };
    },
    mounted() {
        this.getUserInfo()
    },
    // updated(){
    //    this.getUserInfo();
    // },
    methods: {
        getUserInfo() {
            let token = localStorage.getItem("xl-token"); //
            if (!token) {
                setTimeout(() => {
                    this.$router.back();
                }, 800);
                this.$toast("参数错误");
                return;
            }
            this.$axios
                .post("/user/getuserInfo", {
                    token: token,
                })
                .then((res) => {
                    console.log(res);
                    if (res.code == 0) {
                        this.userInfo = res.userInfo;
                        localStorage.setItem('xl-userInfo', JSON.stringify(this.userInfo));
                        this.$root.userInfo = this.userInfo;
                    } else {
                        this.$toast(res.msg);
                    }

                    //  console.log(res)
                });
        },
        async uploadImg(e) {
            let file = e.target.files[0];

            // console.log(file);

            //   1. 需要获取到一个上传前的凭证

            let { token } = await this.getUploadToken();

            // console.log(token);

            let exname = file.name.slice(file.name.lastIndexOf("."));

            let key = `${new Date().getTime()}_${Math.random()
                .toString(36)
                .slice(2)}${exname}`;

            //  2.  三个参数一起发给七牛云的服务器  token   file  文件名

            // 想上传文件  FormData

            let formData = new FormData();

            formData.append("key", key);
            formData.append("token", token);
            formData.append("file", file);

            this.$axios.post("https://upload-z1.qiniup.com", formData).then((res) => {
                console.log(res);

                let avatar = `http://toutiao.longxiaokj.com/${res.key}`;

                this.userInfo.avatar = avatar;
                localStorage.setItem('xl-active', this.userInfo.avatar)
                //  给对象动态的添加属性 并不会触发自动更新
                this.$forceUpdate();
            });


        },

        //  获取上传前的凭证
        getUploadToken() {
            return this.$axios.get(
                "https://d33a5037-6c63-4e92-8bb1-30018ab701ea.bspapp.com/http/upload/token"
            );
        },
        // 提交
        onSubmit() {
            let { nickname, sex, birthday } = this.userInfo;
            let uid = localStorage.getItem('xl-uid');

            // console.log(this.userInfo.avatar)
            let actives = localStorage.getItem('xl-active')
            console.log(actives)

            this.$axios.post('/user/editUserInfo', {
                avatar: actives, nickname, sex, birthday, uid
            })
                .then(res => {
                    console.log(res)
                    if (res.code == 0) {
                        this.$toast('修改成功')
                        this.getUserInfo()

                        setTimeout(() => {
                            this.$router.push('/my')
                        }, 800)
                    } else {    
                        this.$toast('修改失败')
                    }
                })


        },
    },
};
</script>

<style lang="scss" scoped>
.top {
    // margin-top: 20px;
    position: absolute;
    right: -40px;
    top: 20px;
}

.mylist {
    width: 100%;

    .van-cell--large .van-cell__title {
        font-size: 20px;
    }

    .inputs {
        width: 100%;
        // padding: 10px;
        height: 70px;
        line-height: 70px;
        // background-color: cyan;
        position: relative;

        // border-bottom: 1px solid rgb(207, 207, 207);
        .tou01 {
            width: 100%;
            // padding: 10px;
            height: 70px;
            line-height: 70px;
            display: block;
            display: flex;
            // background-color: cyan;
            position: relative;
            border-bottom: 1px solid rgb(207, 207, 207);

            p {
                font-size: 20px;
                width: 20%;
                line-height: 60px;
                margin-left: 15px;
            }

            .icon {
                position: absolute;
                right: 15px;
                top: 24px;
            }

            .dizhi {
                width: 70%;
                height: 90%;
                line-height: 70px;
                text-align: center;
            }
        }
    }

    .tou {
        width: 100%;
        // padding: 10px;
        height: 70px;
        line-height: 70px;
        display: block;
        display: flex;
        // background-color: cyan;
        position: relative;
        border-bottom: 1px solid rgb(207, 207, 207);

        input {
            position: absolute;
            width: 180px;
            height: 60px;
            right: -40px;
            top: 5px;
            text-align: center;
            border-style: none;
        }

        p {
            font-size: 20px;
            margin-left: 15px;
        }

        div {
            width: 50px;
            height: 50px;
            display: flex;
            position: absolute;
            justify-content: space-between;
            line-height: 50px;
            right: 60px;

            img {
                width: 50px;
                height: 50px;
                margin-right: 20px;
                margin-top: 10px;
                border-radius: 50px;
            }
        }
    }
}

.btn {
    width: 200px;
    height: 60px;
    background-color: #07c160;
    color: white;
    text-align: center;
    line-height: 60px;
    margin: 100px auto;
    border-radius: 15px;
}
</style>